

package com.dog.basics.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.sys.*;
import com.dog.basics.bean.model.sys.SysUser;
import com.dog.basics.bean.vo.admin.sys.AdminUserInfoVo;
import com.dog.basics.bean.vo.admin.sys.AdminUserVo;
import com.dog.basics.bean.vo.admin.sys.AdminUserXqVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author guonh
 * @date 2018/12/16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/user")
@Api(value = "用户管理模块", tags = "用户管理模块")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping("/info/{username}")
    @ApiOperation(value = "获取指定用户全部信息", notes = "sys_user_info_username")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @PreAuthorize("@pms.hasPermission('sys_user_info_username')")
    public R<AdminUserVo> info(@PathVariable String username) {
        List<SysUser> list = userService.list(Wrappers.<SysUser>lambdaQuery().select(SysUser::getUserId).eq(SysUser::getUsername, username));
        Validator.validateTrue(CollUtil.isNotEmpty(list), "common.noData!");
        return R.ok(userService.findUserInfo(list.get(0).getUserId()));
    }

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping(value = {"/info"})
    @ApiOperation(value = "获取当前用户全部信息", notes = "sys_user_info")
    @PreAuthorize("@pms.hasPermission('sys_user_info')")
    public R<AdminUserVo> info() {
        return R.ok(userService.findUserInfo(SecurityUtils.getSysUser().getUserId()));
    }

    /**
     * 通过ID查询用户信息
     *
     * @param id ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询用户信息", notes = "sys_user_info_id")
    @ApiImplicitParam(name = "id", value = "用户主键", required = true, dataType = "Long")
    @PreAuthorize("@pms.hasPermission('sys_user_info_id')")
    public R<AdminUserInfoVo> user(@PathVariable Long id) {
        return R.ok(userService.selectUserVoById(id));
    }

    /**
     * 通过ID查询用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/xq")
    @ApiOperation(value = "查询个人详细信息", notes = "sys_user_xq")
    @PreAuthorize("@pms.hasPermission('sys_user_xq')")
    public R<AdminUserXqVo> getObjXq() {
        return R.ok(userService.getObjXq(SecurityUtils.getSysUser().getUserId()));
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return
     */
    @GetMapping("/details/{username}")
    @ApiOperation(value = "根据用户名查询用户信息", notes = "sys_user_details_username")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @PreAuthorize("@pms.hasPermission('sys_user_details_username')")
    public R<Boolean> user(@PathVariable String username) {
        List<SysUser> list = userService.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
        Validator.validateFalse(CollUtil.isNotEmpty(list), "用户已经存在!");
        return R.ok(Boolean.TRUE);
    }

    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @SysLog("删除用户信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_user_del')")
    @ApiOperation(value = "删除用户", notes = "sys_user_del")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
    public R<Boolean> userDel(@PathVariable Long id) {
        SysUser sysUser = userService.getById(id);
        Validator.validateNotNull(sysUser, "common.noData");
        return R.ok(userService.deleteUserById(sysUser));
    }

    /**
     * 添加用户
     *
     * @param obj 用户信息
     * @return success/false
     */
    @SysLog("添加用户")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_user_add')")
    @ApiOperation(value = "添加用户", notes = "sys_user_add")
    public R<Boolean> user(@RequestBody AdminSysUserAddDto obj) {
        return R.ok(userService.saveUser(obj));
    }

    /**
     * 更新用户信息
     *
     * @param obj 用户信息
     * @return R
     */
    @SysLog("更新用户信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_user_edit')")
    @ApiOperation(value = "更新用户信息", notes = "sys_user_edit")
    public R<Boolean> updateUser(@Valid @RequestBody AdminSysUserEditDto obj) {
        return R.ok(userService.updateUser(obj));
    }

    /**
     * 分页查询用户
     *
     * @param page 参数集
     * @param obj  查询参数列表
     * @return 用户集合
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询用户", notes = "sys_user_page")
    @PreAuthorize("@pms.hasPermission('sys_user_page')")
    public R<IPage<AdminUserInfoVo>> getUserPage(PageParam page, AdminSysUserFromDto obj) {
        return R.ok(userService.getUsersWithRolePage(page, obj));
    }

    @GetMapping("/page/code")
    @ApiOperation(value = "角色编号分页用户", notes = "sys_user_page_code")
    @PreAuthorize("@pms.hasPermission('sys_user_page_code')")
    public R<IPage<AdminUserInfoVo>> getUserRoleCodePage(PageParam page, AdminSysUserFromDto obj) {
        return R.ok(userService.getUsersWithRoleCodePage(page, obj));
    }


    /**
     * 修改个人信息
     *
     * @param obj userDto
     * @return success/false
     */
    @SysLog("修改个人信息")
    @PutMapping("/edit")
    @ApiOperation(value = "修改个人信息", notes = "sys_user_editInfo")
    @PreAuthorize("@pms.hasPermission('sys_user_editInfo')")
    public R<Boolean> updateUserInfo(@Valid @RequestBody AdminSysUserEditInfoDto obj) {
        SysUser sysUser = userService.getById(SecurityUtils.getSysUser().getUserId());
        Validator.validateNotNull(sysUser, "common.noData");
        obj.setUsername(sysUser.getUsername());
        return R.ok(userService.updateUserInfo(obj, sysUser));
    }

    /**
     * @param username 用户名称
     * @return 上级部门用户列表
     */
    @GetMapping("/ancestor/{username}")
    @ApiOperation(value = "上级部门用户列表", notes = "sys_user_ancestor")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @PreAuthorize("@pms.hasPermission('sys_user_ancestor')")
    public R<List<AdminUserInfoVo>> listAncestorUsers(@PathVariable String username) {
        return R.ok(userService.listAncestorUsers(username));
    }

    /**
     * @Description: 冻结解冻用户
     * @Param: Boolean
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/9  0027
     */
    @SysLog("冻结解冻用户")
    @ApiOperation(value = "冻结解冻用户", notes = "sys_user_nouse")
    @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true)
    @PutMapping("/nouse/{id}")
    @PreAuthorize("@pms.hasPermission('sys_user_nouse')")
    public R<Boolean> nouse(@PathVariable("id") Long id) {
        return R.ok(userService.nouse(id));
    }

    /**
     * @Description: 是否强制用户修改密码
     * @Param: Boolean
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/9  0027
     */
    @SysLog("是否强制用户修改密码")
    @ApiOperation(value = "是否强制用户修改密码", notes = "sys_user_sfPassword")
    @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true)
    @PutMapping("/sfPassword/{id}")
    @PreAuthorize("@pms.hasPermission('sys_user_sfPassword')")
    public R<Boolean> sfPassword(@PathVariable("id") Long id) {
        return R.ok(userService.sfPassword(id));
    }

    /**
     * 重置密码
     *
     * @param obj
     * @return guonh 2020年2月10日
     */
    @SysLog("重置密码")
    @PutMapping("/editResetPassword")
    @ApiOperation(value = "重置密码", notes = "sys_user_resetpasswordedit")
    @PreAuthorize("@pms.hasPermission('sys_user_resetpasswordedit')")
    public R<Boolean> editResetPassword(@Valid @RequestBody AdminSysUserResetDto obj) {
        return R.ok(userService.editResetPassword(obj));
    }

}
